// miniprogram/pages/order/confirm/confirmOrder.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo: [],
    totalAmount: 0,
    buttons: [
      {
          type: 'primary',
          className: '',
          text: '去添加',
          value: 1
      }
    ],
    show: false,
    tel: '',
    name: '',
    address: '',
    listAddress: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const order = JSON.parse(options.orderInfo);
    const amount = Number(options.totalAmount);
    this.setData({
      orderInfo: order,
      totalAmount: amount
    });
    this.getUserAddress();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserAddress();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  payBtn: function(e) {
    console.log(app.globalData.userInfo, "用户信息pay");
    let recive = {
      name: this.data.name,
      phone: this.data.tel,
      address: this.data.address
    };
    let product = this.data.orderInfo.map((item)=>{
      return {
        buycarId: item._id,
        name: item.name,
        sellPrice: item.sellPrice,
        sellUnit: item.sellUnit,
        num: item.num,
        images: item.images
      }
    });
    let param = {
      receiveInfo: recive,
      no: new Date().getTime(),
      deliverMoney: 0,
      productSumMoney: this.data.totalAmount,
      sumPayMoney: this.data.totalAmount,
      status: '待配送',
      expressNo: '',
      createDate: new Date(),
      userName: app.globalData.userInfo.nickName,
      productList: product
    };
    wx.cloud.callFunction({
      // 需调用的云函数名
      name: 'sallOrderApi',
      // 传给云函数的参数
      data: {
        method: "saveOrder",
        args: param
      },
      // 成功回调
      success: (res)=>{
        console.log("支付订单返回信息", res);
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 1000
        });
        wx.navigateBack({
          delta: 1
        })
      }
    });
  },
  // 选择地址
  saveAddr: function(e) {
    console.log("保存地址", e);
    this.setData({
      show: false
    });
    wx.navigateTo({
      url: '../../address/receiveAddr'
    });
  },
  updateAddr: function(e) {
    console.log("更换地址");
    this.setData({
      show: true
    });
  },
  radioChange: function(e) {
    console.log("改变选中", e, this.data.listAddress);
    const addrList = this.data.listAddress.filter(item => item._id === e.detail.value);
    console.log("过滤后", addrList);
    this.setData({
      name: addrList[0].name,
      tel: addrList[0].tel,
      address: addrList[0].address + addrList[0].addressDetail
    });
  },
  bindInputName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindInputTel: function(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  bindInputAddress: function(e) {
    this.setData({
      address: e.detail.value
    })
  },
  getUserAddress() {
    wx.cloud.callFunction({
      // 需调用的云函数名
      name: 'addressApi',
      // 传给云函数的参数
      data: {
        method: "queryList",
        args: {}
      },
      // 成功回调
      success: (res)=>{
        console.log("获取地址成功", res);
        const data = res.result.rstData.data;
        this.setData({
          name: data[0].name,
          tel: data[0].tel,
          address: data[0].address + data[0].addressDetail,
          listAddress: data
        });
      }
    });
  }
})